package com.ilop.sthome.ui.adapter.device;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.ilop.sthome.data.enums.SmartDevice;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.bean.HistoryBean;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.DeviceStatusUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.example.common.utils.DateUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-01-13.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：子设备详细告警信息
 */
public class SubHistoryDetailAdapter extends RecyclerView.Adapter<SubHistoryDetailAdapter.ItemHolder> {

    private Context mContext;
    private List<HistoryBean> mList;

    public SubHistoryDetailAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setList(List<HistoryBean> mList) {
        this.mList = mList;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_sub_history, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        HistoryBean mWarnBean = mList.get(i);
        DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findByDeviceId(mWarnBean.getDeviceName(),mWarnBean.getDevice_id());
        String subDeviceName = mContext.getResources().getString(SmartDevice.getType(mWarnBean.getDevice_type()).getTypeStrId())+mWarnBean.getDevice_id();
        if (deviceInfoBean!=null){
            if(!TextUtils.isEmpty(deviceInfoBean.getSubdeviceName())){
                subDeviceName = deviceInfoBean.getSubdeviceName();
            }
        }
        String title;
        if (SmartDevice.EE_DEV_TH_CHECK == SmartDevice.getType(mWarnBean.getDevice_type())){
            title = subDeviceName +  "\t\t" + CoderUtils.getHumitureMsg(mContext, mWarnBean.getDevice_type(), mWarnBean.getDevice_status());
        }else {
            title = subDeviceName +  "\t\t" + DeviceStatusUtil.getAlert(mContext,mWarnBean.getDevice_type(),mWarnBean.getDevice_status());
        }
        itemHolder.mTime.setText(DateUtil.getDateToString(mWarnBean.getTime()*1000));
        itemHolder.mStatus.setText(title);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_status)
        TextView mStatus;
        @BindView(R.id.tv_time)
        TextView mTime;

        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
